Skip to content

Conversation

agamdua
Copy link
Contributor

@agamdua agamdua commented Oct 11, 2024

Motivation:

As an HTTP library, async-http-client should have authentication support.

Modifications:

This adds a setBasicAuth() method to both HTTPClientRequest and HTTPClient.Request and their related unit tests.

Result:

Library users will be able to leverage this method to use basic authentication on their requests without implementing this in their own projects.

Note: I also ran the tests (swift test) with the docker.io/library/swift:6.0-focal and docker.io/library/swift:5.10.1-focal to ensure linux compatibility.

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Oct 14, 2024
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @agamdua! I've left a few notes in the diff. I'm broadly happy with this, but I'd like to see if @FranzBusch thinks the API is suitable for now.

//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors
// Copyright (c) 2024 Apple Inc. and the AsyncHTTPClient project authors

/// - username: the username to authenticate with
/// - password: authentication password associated with the username
mutating public func setBasicAuth(username: String, password: String) {
let value = Data(String(format: "%@:%@", username, password).utf8)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can construct this a bit more cheaply. Something like this:

var value = Data()
value.reserveCapacity(username.utf8.count + password.utf8.count + 1)
value.append(contentsOf: username.utf8)
value.append(UInt8(ascii: ":"))
value.append(contentsOf: password.utf8)

mutating public func setBasicAuth(username: String, password: String) {
let value = Data(String(format: "%@:%@", username, password).utf8)
let encoded = value.base64EncodedString()
self.headers.replaceOrAdd(name: "Authorization", value: "Basic \(encoded)")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor out the common code here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new file and function for the code here, and extended HTTPHeaders with internal access to reuse the code effectively. Let me know if you prefer different code organization!

@Lukasa
Copy link
Collaborator

Lukasa commented Oct 14, 2024

@swift-server-bot add to allowlist

Copy link
Collaborator

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking this looks good to me. Just the comments that @Lukasa added.

@agamdua
Copy link
Contributor Author

agamdua commented Oct 15, 2024

Sounds good - thank you both! Will address comments soon, they all make sense 👍

@agamdua agamdua force-pushed the agam-basic-auth branch 2 times, most recently from a3387b1 to 44fb720 Compare October 21, 2024 03:46
Motivation:

As an HTTP library, async-http-client should have authentication support.

Modifications:

This adds a `setBasicAuth()` method to both `HTTPClientRequest` and
`HTTPClient.Request` and their related unit tests.

Result:

Library users will be able to leverage this method to use
basic authentication on their requests without implementing
this in their own projects.

Signed-off-by: Agam Dua <[email protected]>
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, thanks @agamdua!

@Lukasa Lukasa merged commit acaca2d into swift-server:main Oct 21, 2024
6 of 7 checks passed
@agamdua agamdua deleted the agam-basic-auth branch October 21, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants